//elements to loop though and add autocomplete for

elements = [{
	"class" : "bf_country",
	"url" : "country",
	"value" : "row.Country",
	"result" : "row.Country",
	"format" : 'item.Country + " [" + item.UNCountryNumber + "]"'
},{
	"class" : "bf_continent_sv",
	"url" : "continent/sv",
	"value" : "row.Svenska",
	"result" : "row.Svenska",
	"format" : "item.Svenska"
},{
	"class" : "bf_continent_en",
	"url" : "continent/en",
	"value" : "row.Engelska",
	"result" : "row.Engelska",
	"format" : "item.Engelska"
},{
	"class" : "bf_region_sv",
	"url" : "region/sv",
	"value" : "row.FN_region_svenska",
	"result" : "row.FN_region_svenska",
	"format" : "item.FN_region_svenska"
},{
	"class" : "bf_region_en",
	"url" : "region/en",
	"value" : "row.FN_region_engelska",
	"result" : "row.FN_region_engelska",
	"format" : "item.FN_region_engelska"
},{
},{
	"class" : "bf_subregion_sv",
	"url" : "subregion/sv",
	"value" : "row.FN_subregion_svenska",
	"result" : "row.FN_subregion_svenska",
	"format" : 'item.FN_subregion_svenska + " (" + item.FN_region_svenska + ")"'
},{
	"class" : "bf_subregion_en",
	"url" : "subregion/en",
	"value" : "row.FN_subregion_engelska",
	"result" : "row.FN_subregion_engelska",
	"format" : 'item.FN_subregion_engelska + " (" + item.FN_region_engelska + ")"'
},{
	"class" : "bf_area",
	"url" : "area",
	"value" : "row.ISO_3166_2_Subdivision_Name",
	"result" : "row.ISO_3166_2_Subdivision_Name",
	"format" : 'item.ISO_3166_2_Subdivision_Name + " (" + item.Country + ")"'
},{
	"class" : "bf_ocean_sv",
	"url" : "ocean/sv",
	"value" : "row.sv",
	"result" : "row.sv",
	"format" : 'item.sv'
},{
	"class" : "bf_ocean_en",
	"url" : "ocean/en",
	"value" : "row.en",
	"result" : "row.en",
	"format" : 'item.en'
},{
	"class" : "bf_island",
	"url" : "island",
	"value" : "row.island",
	"result" : "row.island",
	"format" : 'item.island + " (" + item.country_region + ")"'
},{
	"class" : "bf_island_group",
	"url" : "island_group",
	"value" : "row.Group",
	"result" : "row.Group",
	"format" : 'item.Group + " (" + item.Country + ")"'
},{
	"class" : "bf_swedish_lan",
	"url" : "swedish_lan",
	"value" : "row.Lan",
	"result" : "row.Lan",
	"format" : 'item.Lan'
},{
	"class" : "bf_swedish_municipality",
	"url" : "swedish_municipality",
	"value" : "row.Kommun",
	"result" : "row.Kommun",
	"format" : 'item.Kommun + " (" + item.Lan + ")"'
},{
	"class" : "bf_swedish_marine_area",
	"url" : "swedish_marine_area",
	"value" : "row.Havsområde",
	"result" : "row.Havsområde",
	"format" : 'item.Havsområde + " (" + item.SMHI_område + ")"'
}];


$(document).ready(function() {
	
	addPreFetchedAutoComplete();

	$(elements).each( function(){
		$.map($(this), function(element){
			addLiveAutoComplete( element );			
		});
	});

});

function addLiveAutoComplete(element){
	
	$("." + element["class"]).autocomplete("remotecall.php?url=" + element.url, 
			{
		dataType : "json",
		parse : function(data) {
		return $.map(data, function(row) {
			return {
				data : row,
				value : eval(element.value),
				result : eval(element.result)
			};
		});
	},
	formatItem : function(item) {
		return eval(element.format);
	},
	max : 20,
	scrollHeight : 200,
	width : 400,
	delay: 0
			});
}

function addPreFetchedAutoComplete(){
	countries = new Array();
	countryValues = new Array();
	unNames = new Array();
	$.getJSON(("remotecall.php?url=" + "country"), function(data) {
		for ( var item in data) {
			countries.push(data[item]["Country"]);
			countryValues.push(data[item]["UNCountryNumber"]);
			unNames.push(data[item]["UNCountryName"]);
		}
		countries.sort();
		$("#land").autocomplete(countries, {
			matchContains : true,
			max : 0,
			scrollHeight : 200,
			width : 260
		});
	});
}


